<?php
/**
 * 角色类
 */
namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\admin\model\Rolemodel;
use app\admin\model\Columnmodel;
use app\admin\validate\Role as validate;

class Role extends Common
{
    private $roleModel = '';
    protected $columnModel = '';

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->localModel = new Rolemodel();
        parent::__construct($app);
        $this->columnModel = new Columnmodel();
    }

    public function index()
    {
        $res = $this->localModel->findAllInfo('id, name, create_date, status');
        View::assign(['roleData'=>$res]);
        return view();
    }


    public function add(){
        $id = $this->request->get('id', 0);

        //获取栏目列表
        $res = $this->columnModel->findAllInfo('id, name, level, father_id, action_id, icon, url', [['id', '<>', 1], ['father_id', 'not in', '1, 6'], ['status', '=', 1]]);

        if($id){
            $roleInfo = $this->localModel->findDetaiInfoById('*', $id);
            $idsArr = explode(',', $roleInfo['role_column']);
        }

        $cloumnArr = $fatherData = $data = array();
        foreach($res as $val){
            $fatherData[$val['id']] = $val;
        }

        $actionArr = json_decode($this->handler->hget('action', 'action_0'), true);

        foreach($fatherData as $key=>$val){

            if(!$val['father_id']){
                $cloumnArr[$key]['id'] = $val['id'];
                $cloumnArr[$key]['pId'] = 0;
                $cloumnArr[$key]['name'] = $val['name'];
                $cloumnArr[$key]['open'] = true;
                if($id && in_array($key, $idsArr))  $cloumnArr[$key]['checked'] = true;
            }else{
                for($i=0; $i<=count($actionArr); $i++){
                    if(!$i || in_array($i, explode(',', $val['action_id']))){
                        $cloumnArrKey = $i ? $key . '_' . $i : $key;
                        $cloumnArr[$cloumnArrKey]['id'] = $cloumnArrKey;
                        $cloumnArr[$cloumnArrKey]['pId'] = $i ? $key : $val['father_id'];
                        $cloumnArr[$cloumnArrKey]['name'] = $i ? $actionArr[$i] : $val['name'];
                        if($val['level'] == 3)  unset($cloumnArr[$val['father_id'].'_'.$i]);

                        if($id && in_array($cloumnArrKey, $idsArr))  $cloumnArr[$cloumnArrKey]['checked'] = true;
                    }

                }

            }
        }//print_r($cloumnArr);

        if($id){
            View::assign(['roleInfo'=>$roleInfo, 'id'=>$id, 'cloumn'=>$cloumnArr]);
            return view('edit');
        }else{
            View::assign(['cloumn'=>$cloumnArr]);
            return view('add');
        }
    }

    //保存
    public function save(){
        $dataArr = $this->request->request('role/a', []);
        $id = $this->request->post('id', 0);

        if($msg = $this->checkData($dataArr))  return json(['result'=>'fail', 'title'=>$msg]);

        $res = $this->localModel->saveInfo($dataArr, $id);

        if(!$res)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        event('ChangeRoleColumn');
        $this->logsData = ['cid'=>$id ? $id : $res, 'action'=>$id ? 'edit' : 'add', 'msg'=>$dataArr['name']];
        return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>(string)url('Role/index')]);
    }

    //删除
    public function delRole(){
        $id = $this->request->post('id', 0);

        if(!$id)  return json(array('result'=>'fail', 'title'=>'参数不能为空！'));

        //删除角色
        $delRes = $this->localModel->delRole($id);
        if(!$delRes)  return json(array('result'=>'fail', 'title'=>'操作失败！'));

        $this->logsData = ['cid'=>$id, 'action'=>'del',];
        return json(array('result'=>'success'));
    }



}
